package com.kotlin.flong.flow

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking

/**
 * @Author:liangjl
 * @Date: 2020/7/11-10:03
 * @Eamil:jilongliang@sina.com
 * @Description:
 * Flow Context (Flow 上下文)
 * Flow的收集动作总是发生在调用协程的上下文当中,这个特性叫做Context Preservation(上下文保留)
 *
 */
private fun log(logMsg: String) = println("[${Thread.currentThread().name}] $logMsg")

private fun myMethod(): Flow<Int> = flow {
    log("start ")

    for (i in 1..3) {
        emit(i)
    }
}

fun main() = runBlocking {
    myMethod().collect { log("collect $it") }
}